package com.xugongzhang.service;

import com.alibaba.fastjson.JSON;
import com.xugongzhang.dto.Message;
import com.xugongzhang.dto.User;
import com.xugongzhang.enums.MessageTypeEnum;
import com.xugongzhang.thread.ManageServerConnectClientThread;
import com.xugongzhang.thread.ServerConnectClientThread;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

/**
 * @author X
 * @date 2022/11/26 22:27
 */
public class QQServer {
    
    private ServerSocket serverSocket = null;
    
    // 创建一个集合存放多个用户
    private static HashMap<String, User> validUsers = new HashMap<>();
    // 在静态代码块初始化
    static {
        validUsers.put("100", new User("100", "123456"));
        validUsers.put("200", new User("200", "123456"));
        validUsers.put("300", new User("200", "123456"));
        validUsers.put("至尊宝", new User("至尊宝", "123456"));
        validUsers.put("紫霞仙子", new User("紫霞仙子", "123456"));
    }
    
    private boolean checkUser(String username, String password) {
        User user = validUsers.get(username);
        if (user != null) {
            if (password.equals(user.getPassword())) {
                return true;
            }
            return false;
        }
        return false;
    }

    public static void main(String[] args) {
        new QQServer();
    }

    public QQServer() {
        System.out.println("服务器在9999端口监听");
        try {
            new Thread(new SendNewsToAllService()).start();
            serverSocket = new ServerSocket(9999);
            // 当和某个客户端连接后，一直监听
            while (true) {
                Socket accept = serverSocket.accept();
                ObjectInputStream ois = new ObjectInputStream(accept.getInputStream());
                ObjectOutputStream oos = new ObjectOutputStream(accept.getOutputStream());
                User user = (User) ois.readObject();
                Message message = new Message();
                if (checkUser(user.getUserId(), user.getPassword())) {
                    System.out.println(JSON.toJSONString(user) + "登陆成功");
                    message.setMsgType(MessageTypeEnum.LOGIN_SUCCEED.getMessageType());
                    oos.writeObject(message);
                    ServerConnectClientThread myThread = new ServerConnectClientThread(accept, user.getUserId());
                    myThread.start();
                    // 把线程对象放在集合中管理
                    ManageServerConnectClientThread.addServerConnectClientThread(user.getUserId(), myThread);
                }
                else {
                    System.out.println(JSON.toJSONString(user) + "登陆失败");
                    message.setMsgType(MessageTypeEnum.LOGIN_FAIL.getMessageType());
                    oos.writeObject(message);
                    accept.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
